package hotel.controller.room;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;

import java.text.DecimalFormat;
import java.text.ParsePosition;

import hotel.common.MsgBox;
import hotel.modal.RoomType;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;

import javafx.scene.control.ChoiceBox;

public class RoomTypeChangeController {
	@FXML
	private Label titleLabel;
	@FXML
	private TextField typeNameField;
	@FXML
	private TextField bedNumField;
	@FXML
	private TextField depositField;
	@FXML
	private TextField priceField;
	@FXML
	private ChoiceBox<String> hourRoomCh;
	@FXML
	private TextField typeIdField;
	@FXML
	private Button btnOk;
	@FXML
	private Button btnCancel;
	
	
	private RoomType roomType = null;
	private boolean addMode = false;
	
	
	@FXML
	public void initialize() {
		
		bedNumField.textProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if (!newValue.matches("\\d*")) {
					bedNumField.setText(newValue.replaceAll("[^\\d]", ""));
				}
			}
		});
		
		DecimalFormat format = new DecimalFormat( "#.0" );
		depositField.setTextFormatter(new TextFormatter<>(c -> {
			if (c.getControlNewText().isEmpty())
				return c;
			ParsePosition parsePosition = new ParsePosition(0);
			Object object = format.parse(c.getControlNewText(), parsePosition);
			if (object == null || parsePosition.getIndex() < c.getControlNewText().length())
				return null;
			else
				return c;
		}));
		
		priceField.setTextFormatter(new TextFormatter<>(c -> {
			if (c.getControlNewText().isEmpty())
				return c;
			ParsePosition parsePosition = new ParsePosition(0);
			Object object = format.parse(c.getControlNewText(), parsePosition);
			if (object == null || parsePosition.getIndex() < c.getControlNewText().length())
				return null;
			else
				return c;
		}));
		
	}

	public void setInitData(RoomType roomType) {
		this.roomType = roomType;
		if(roomType != null) {
			if(!addMode) {
				typeNameField.setText(roomType.getTypeName());
				typeIdField.setText(String.valueOf(roomType.getTypeId()));
				bedNumField.setText(String.valueOf(roomType.getBedNum()));
				depositField.setText(String.format("%.2f", roomType.getDeposit()));
				priceField.setText(String.format("%.2f", roomType.getPrice()));
				if(roomType.isHourRoom()) hourRoomCh.getSelectionModel().select("是");
				else hourRoomCh.getSelectionModel().select("否");
			}else {
				typeIdField.setText(String.valueOf(roomType.getTypeId()));
				typeNameField.requestFocus();
				titleLabel.setText("房间类型添加");
			}
		}
		this.roomType.setModified(false);
	}
	
	public void setAddMode() {
		this.addMode = true;
	}
	
	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		boolean check = true;
		String typeName = typeNameField.getText();
		String bedNum = bedNumField.getText();
		String deposit = depositField.getText();
		String price = priceField.getText();
		
		if(check && typeName.length() > 20) {
			check = false;
			MsgBox.showErrorDialog("错误", null, "房间类型名称过长！");
			typeNameField.requestFocus();
		}
		if(check && (bedNum == null || bedNum.equals(""))) {
			check = false;
			MsgBox.showErrorDialog("错误", null, "必须输入床位数量！");
			bedNumField.requestFocus();
		}
		if(check && (deposit == null || deposit.equals(""))) {
			check = false;
			MsgBox.showErrorDialog("错误", null, "必须输入押金金额！");
			depositField.requestFocus();
		}
		if(check && (price == null || price.equals(""))) {
			check = false;
			MsgBox.showErrorDialog("错误", null, "必须输入房间价格！");
			priceField.requestFocus();
		}
		if(check) {
			roomType.setTypeName(typeName);
			roomType.setBedNum(Integer.parseInt(bedNum));
			roomType.setDeposit(Double.parseDouble(deposit));
			roomType.setPrice(Double.parseDouble(price));
			if(hourRoomCh.getSelectionModel().getSelectedItem().equals("是")) roomType.setHourRoom(true);
			else roomType.setHourRoom(false);
			roomType.setModified(true);
			closeWindow();
		}
		check = true;
	}
	
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		roomType.setModified(false);
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage)btnCancel.getScene().getWindow();
		stage.close();
	}
}
